package executors;
import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Test;

import test.TestRandomReceivers;


public class JunitRandomReceivers
{
	@Test
	// Test that 0 max receivers will get us nothing
	public void test()
	{
		ArrayList<Integer> retval = new ArrayList<Integer>();
		ArrayList<Integer> neighbors = new ArrayList<Integer>();
		ArrayList<Integer> alreadyReceived = new ArrayList<Integer>();
		
		neighbors.add(1);neighbors.add(2);neighbors.add(3);
		alreadyReceived.add(4);
		assertEquals(0, retval.size());
		retval = TestRandomReceivers.fetchRandomRecievers(neighbors, 0, alreadyReceived);
		assertEquals(0, retval.size());
	}
	
	@Test
	// Test that no one new will be added if all already received
	public void test2()
	{
		ArrayList<Integer> retval = new ArrayList<Integer>();
		ArrayList<Integer> neighbors = new ArrayList<Integer>();
		ArrayList<Integer> alreadyReceived = new ArrayList<Integer>();
		
		neighbors.add(1);neighbors.add(2);neighbors.add(3);
		alreadyReceived.add(1);alreadyReceived.add(2);alreadyReceived.add(3);
		assertEquals(0, retval.size());
		retval = TestRandomReceivers.fetchRandomRecievers(neighbors, 1, alreadyReceived);
		assertEquals(0, retval.size());
	}

	@Test
	// Test that no more than the required will be added
	public void test3()
	{
		ArrayList<Integer> retval = new ArrayList<Integer>();
		ArrayList<Integer> neighbors = new ArrayList<Integer>();
		ArrayList<Integer> alreadyReceived = new ArrayList<Integer>();
		
		neighbors.add(1);neighbors.add(2);neighbors.add(3);
		alreadyReceived.add(4);alreadyReceived.add(5);
		assertEquals(0, retval.size());
		retval = TestRandomReceivers.fetchRandomRecievers(neighbors, 1, alreadyReceived);
		assertEquals(1, retval.size());
		assertTrue((retval.get(0) == 1) || (retval.get(0) == 2) || (retval.get(0) == 3));
	}
	
	@Test
	// Just test to see if it works OK in general
	public void test4()
	{
		ArrayList<Integer> retval = new ArrayList<Integer>();
		ArrayList<Integer> neighbors = new ArrayList<Integer>();
		ArrayList<Integer> alreadyReceived = new ArrayList<Integer>();
		
		neighbors.add(1);neighbors.add(2);neighbors.add(3);
		neighbors.add(4);neighbors.add(5);neighbors.add(6);
		neighbors.add(7);neighbors.add(8);neighbors.add(9);
		neighbors.add(10);neighbors.add(11);neighbors.add(12);
		alreadyReceived.add(1);alreadyReceived.add(5);
		alreadyReceived.add(9);alreadyReceived.add(11);
		assertEquals(0, retval.size());
		retval = TestRandomReceivers.fetchRandomRecievers(neighbors, 4, alreadyReceived);
		assertEquals(4, retval.size());
		ArrayList<Integer> possibleOptions = new ArrayList<Integer>();
		possibleOptions.add(2);
		possibleOptions.add(3);
		possibleOptions.add(4);
		possibleOptions.add(6);
		possibleOptions.add(7);
		possibleOptions.add(8);
		possibleOptions.add(10);
		possibleOptions.add(12);
		assertTrue((retval.get(0) != retval.get(1)) && (retval.get(0) != retval.get(2)) && (retval.get(0) != retval.get(3)));
		assertTrue((retval.get(1) != retval.get(2)) && (retval.get(1) != retval.get(3)));
		assertTrue((retval.get(2) != retval.get(3)));
		assertTrue(possibleOptions.containsAll(retval));
	}
}
